package com.hp.bon.sgw.util;

import java.net.URL;

import org.jboss.resteasy.plugins.server.tjws.TJWSEmbeddedJaxrsServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TJWSHelper {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(TJWSHelper.class);

	private final TJWSEmbeddedJaxrsServer tjws;
	private String rootResourcePath = "/man";
	private int port;
	private String manURL = null;

	public TJWSHelper() {
		tjws = new TJWSEmbeddedJaxrsServer();

	}

	public void start() throws Exception {
		logger.info("start tjws manager server...");
		if (manURL == null) {
			manURL = "http://0.0.0.0:" + port + rootResourcePath;
		}
		URL url = new URL(manURL);
		String host = url.getHost();
		if (!host.equals("localhost") && !host.equals("127.0.0.1")) {
			tjws.setBindAddress(host);
		}
		logger.info("start tjws manager server:" + manURL);
		tjws.setKeepAlive(false);
		tjws.setThreadPoolSize(8);
		tjws.setSessionTimeout(10);
		tjws.setPort(url.getPort());
		tjws.setRootResourcePath(rootResourcePath);
		try {
			tjws.start();
		} catch (Exception e) {
			logger.warn(e.toString());
		}
	}

	public void stop() {
		tjws.stop();
		logger.info("stopped tjws manager server:" + rootResourcePath);
	}

	public void setRootResourcePath(String rootResourcePath) {
		this.rootResourcePath = rootResourcePath;
	}

	public TJWSEmbeddedJaxrsServer getTjws() {
		return tjws;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setManURL(String manURL) {
		this.manURL = manURL;
	}

 
}
